﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SplitMessageConverter.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   Value Converter for the Formatting of an AutomationML Message <see cref="AutomationMLMessage" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.Controls.ConvertersAndTemplateSelectors
{
    using System;
    using System.Globalization;
    using System.Windows.Data;

    using AutomationMLMapper.ViewModels;

    /// <summary>
    ///   Value Converter for the Formatting of an AutomationML Message <see cref="AutomationMLMessage" />
    /// </summary>
    public class SplitMessageConverter : IValueConverter
    {
        #region Public Methods and Operators

        /// <summary>
        /// convert method for a notification method used for string formating
        /// </summary>
        /// <param name="value">
        /// </param>
        /// <param name="targetType">
        /// </param>
        /// <param name="parameter">
        /// </param>
        /// <param name="culture">
        /// </param>
        /// <returns>
        /// The System.Object. 
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is String)
            {
                int part;
                if (int.TryParse(parameter as String, out part))
                {
                    var splits = (value as string).Split(new[] { ':' }, 2);
                    return splits.Length >= part + 1 ? splits[part] : value;
                }
            }

            return value;
        }

        /// <summary>
        /// backward conversion (not used)
        /// </summary>
        /// <param name="value">
        /// </param>
        /// <param name="targetType">
        /// </param>
        /// <param name="parameter">
        /// </param>
        /// <param name="culture">
        /// </param>
        /// <exception cref="NotImplementedException">
        /// </exception>
        /// <returns>
        /// The System.Object. 
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}